package com.stars.pao.springbook.controller;

import com.stars.pao.springbook.constants.Constants;
import com.stars.pao.springbook.model.UserInfo;
import com.stars.pao.springbook.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session) {
//        校验参数
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userInfo = userService.queryByName(userName);

        if(userInfo == null) {
            return false;
        }

        if(userInfo.getPassword().equals(password)) {
            userInfo.setPassword("");
            session.setAttribute("user_info_key", userInfo);
            return true;
        }
//        JWT生成

//        校验密码是否正确
//        if("admin".equals(userName) && "admin".equals(password)) {
////            存session
//            session.setAttribute("userName", userName);
//            return true;
//        }
        return false;
    }
}
